#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   tree_helper.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/5/27 16:29   小钟同学      1.0         None
'''


def generate_tree(data, parent_id=None):
    # 根据父id递归生成树状结构
    children = []
    for item in data:
        if item['parentId'] == parent_id:
            subchildren = generate_tree(tuple(data), item['menuId'])
            if subchildren:
                item['children'] = subchildren
            children.append(item)
    return children



def get_name(data, item):
    """
    递归获取名称
    """
    # 如果 parent_id 等于自己的 id，则说明已经遍历到了根节点，返回节点名称
    if item['parent_id'] == item['id']:
        return item['name']
    # 否则递归获取父节点名称
    for d in data:
        if d['id'] == item['parent_id']:
            return get_name(data, d) + '/' + item['name']
    return ''

def convert_data(data):
    """
    转换数据
    """
    result = []
    for item in data:
        name = get_name(data, item)
        result.append({'id': item['id'], 'name': name})
    return result

